#include "MineDesign3D/ConfigIO.hpp"
#include <algorithm>
#include <fstream>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

namespace MineDesign3D{
static const char* CONFIG_FILENAME = "Config.cfg";

ConfigIO::ConfigIO():
 m_backColor()
{
	std::copy(DEFULT_BACK_COLOR.begin(),DEFULT_BACK_COLOR.end(),m_backColor.begin());
}
void ConfigIO::load()
{
	std::ifstream ifs(CONFIG_FILENAME);
	if(ifs.fail())
	{
		return;
	}

	boost::property_tree::ptree rootNode;
	boost::property_tree::read_json(ifs,rootNode);

	boost::property_tree::ptree backColorNode = rootNode.get_child("backColor");

	m_backColor[0] = backColorNode.get<float>("r");
	m_backColor[1] = backColorNode.get<float>("g");
	m_backColor[2] = backColorNode.get<float>("b");
}
void ConfigIO::save()
{
	boost::property_tree::ptree rootNode;
	
	boost::property_tree::ptree backColorNode;
	backColorNode.put("r",m_backColor[0]);
	backColorNode.put("g",m_backColor[0]);
	backColorNode.put("b",m_backColor[0]);

	rootNode.add_child("backColor",backColorNode);

	std::ofstream ofs(CONFIG_FILENAME);
	boost::property_tree::write_json(ofs,rootNode);
}
void ConfigIO::setBackColor(float r,float g,float b)
{
	m_backColor[0] = r;
	m_backColor[1] = g;
	m_backColor[2] = b;
}
ConfigIO::Color ConfigIO::getBackColor()const
{
	return boost::make_tuple(m_backColor[0],m_backColor[1],m_backColor[2]);
}
}//MineDesign3D